package com.pay.ioopos.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.pay.ioopos.DataFactory;
import com.pay.ioopos.DataStore;
import com.pay.ioopos.KeyCodeFactory;
import com.pay.ioopos.KeyInfo;
import com.pay.ioopos.PayMode;
import com.pay.ioopos.R;

import java.math.BigDecimal;
import java.math.RoundingMode;


public class ModeAmountFragment extends Fragment implements View.OnKeyListener {
    private Handler handler = new Handler();
    private DataStore dataStore = DataFactory.getDataStore();
    private EditText amountView;
    private PayMode mode;
    private OnAmountListner listner;
    public ModeAmountFragment(PayMode mode, OnAmountListner listner) {
        this.mode = mode;
        this.listner = listner;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mode_amount, container, false);
        amountView = view.findViewById(R.id.input_model_amount);
        amountView.setOnKeyListener(this);
        TextView textView = view.findViewById(R.id.text_mode_typein);
        if (mode == PayMode.NORMAL) {
            textView.setText("输入金额上限");
            amountView.setText(dataStore.getMaxAmount());
        } else {
            textView.setText("输入固定金额");
            amountView.setText(dataStore.getFixAmount());
        }
        return view;

    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() != KeyEvent.ACTION_UP) {
            return false;
        }

        KeyInfo keyInfo = KeyCodeFactory.getKeyInfo(event.getKeyCode());
        if (null == keyInfo ) {
            return false;
        }
        if (keyInfo == KeyInfo.KEY_CANCEL) {
            listner.onCancel();
        } if (keyInfo == KeyInfo.KEY_DELETE) {
            amountView.setText("");
        } else if (keyInfo == KeyInfo.KEY_ENTER) {
            checkAmount();
        }
        return false;
    }

    private void checkAmount() {
        BigDecimal amount;
        try {
            amount = new BigDecimal(amountView.getText().toString()).setScale(2, RoundingMode.DOWN);
            if (amount.compareTo(BigDecimal.ZERO) != 1) {
                throw new NumberFormatException();
            }
            listner.onFinish(amount.toPlainString());
        } catch (NumberFormatException e) {
            handler.post(() -> {
                Toast.makeText(getContext(), "金额格式错误", Toast.LENGTH_SHORT).show();
            });
            amountView.setText("");
        }
    }

    public interface OnAmountListner {
        void onFinish(String amount);
        void onCancel();
    }

}
